#include <fstream>
#include <ostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main(){
    
	string data[97][88];
	string ccbp = "CCBP";
	string pnp = "PNP";
	string g3p = "G3P";
    FILE *ifile = fopen("scada.txt","r");
	if(NULL == ifile){
		fprintf(stderr,"Fail to open file .\n");
		return -1;
	}
    ifstream infile("scada.txt");
	int  m, n;
	int ccbp_num = 0;
	int pnp_num = 0;
	int g3p_num = 0;
    for( m = 0; m < 97; m++)
	{
		for (n = 0;n < 86;n++)
		{
			getline(infile, data[m][n], ',');
		}		
		getline(infile, data[m][87], '\n');
	}
    for (n = 0;n < 88;n++)
	{
		if(strcmp(data[0][n].c_str(),ccbp.c_str()) == 0)
			ccbp_num = n;
		if(strcmp(data[0][n].c_str(),pnp.c_str()) == 0)
			pnp_num = n;
		if(strcmp(data[0][n].c_str(),g3p.c_str()) == 0)
			g3p_num = n;
	
	}
	printf("DATE\tTIME\tCCBP\tPNP\tG3P\t\n");
	for (int i = 1; i < 97; ++i) {
		printf( "%s\t%s\t%s\t%s\t%s\t\n",data[i][0].c_str(),data[i][1].c_str(), data[i][ccbp_num].c_str(),data[i][pnp_num].c_str(),data[i][g3p_num].c_str());
	}
	float sum_ccbp_f,sum_pnp_f,sum_g3p_f,avg_ccbp_f,avg_pnp_f,avg_g3p_f;
	sum_ccbp_f = sum_pnp_f = sum_g3p_f = avg_ccbp_f = avg_pnp_f = avg_g3p_f = 0;
	double sum_ccbp_d,sum_pnp_d,sum_g3p_d,avg_ccbp_d,avg_pnp_d,avg_g3p_d;
	sum_ccbp_d = sum_pnp_d = sum_g3p_d = avg_ccbp_d = avg_pnp_d = avg_g3p_d = 0;

	for (int i = 1;i < 97;i++)
	{
		sum_ccbp_f = sum_ccbp_f + atof(data[i][ccbp_num].c_str());
		sum_pnp_f = sum_pnp_f + atof(data[i][pnp_num].c_str());
		sum_g3p_f = sum_g3p_f + atof(data[i][g3p_num].c_str());
		sum_ccbp_d = sum_ccbp_d + atof(data[i][ccbp_num].c_str());
		sum_pnp_d = sum_pnp_d + atof(data[i][pnp_num].c_str());
		sum_g3p_d = sum_g3p_d + atof(data[i][g3p_num].c_str());
	}
	avg_ccbp_f = sum_ccbp_f/96;
	avg_pnp_f = sum_pnp_f/96;
	avg_g3p_f = sum_g3p_f/96;
	avg_ccbp_d = sum_ccbp_d/96;
	avg_pnp_d = sum_pnp_d/96;
	avg_g3p_d = sum_g3p_d/96;
	printf("CCBP_FLOAT_SUM\tCCBP_DOUBLE_SUM\n");
	printf("%15.9f\t%15.9f\t",sum_ccbp_f,sum_ccbp_d);
	infile.close();
    return 0;
}